<?php

if (!defined('BASEPATH'))
  exit('No direct script access allowed');

class Agent extends MY_Controller {
	
	var $sip_file_path = '';
	var $agent_file_path = '';
	var $log_file_name = '';
	

	function __construct() {
		parent::__construct();
		$this->require_login();
		$this->load->model('skill_model', 'skill');
		$this->load->model('agent_model', 'agent');
		
		/*//$this->sip_file_path = getRootPath()."DGSSIP.conf";
		//$this->agent_file_path = getRootPath()."agents.conf";
		
		$this->sip_file_path = "/etc/asterisk/DGSSIP.conf";
		$this->agent_file_path = "/etc/asterisk/agents.conf";
		
		if( ! file_exists($this->sip_file_path) ) {
			$this->sip_file_path = getRootPath()."DGSSIP.conf";
		}
		if( ! file_exists($this->agent_file_path) ) {
			$this->agent_file_path = getRootPath()."agents.conf";
		}
		
		
		$this->log_file_name = getRootPath()."log/dgssipp.text";*/
	}

	function index() 
	{
		check_permission("agent", "view");
		
		$data = array();
		$data['page_heading'] = 'Agents';
		$GET = $this->input->get(NULL, true);
 		$this->config->set_item('enable_query_strings', TRUE);
		$config['base_url'] = base_url().$this->uri->segment(1).'/?';
		$config['per_page'] = '20';
 		$config['num_links'] = 5;	
		$start = $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$results = $this->agent->getListingData($GET,$start);
		$data['start'] = $start;
		$data['results'] = $results['results'];
		$data['total_record'] = $results['total_record'];
		
		if(is_array(@$GET))	{
			unset($GET['per_page']);
 			$qs = http_build_query($GET); 
		} else {
			$qs = '';	
		}
		
		if(isset($qs) && !empty($qs)){
		   $this->pagination->suffix = "&".$qs;	
		}
		
		$config['total_rows'] = $data['total_record'];
		$config['full_tag_open'] = '<ul class="paging">';
		$config['full_tag_close'] = '</ul>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		
		$perPage = $config['per_page'];
		$this->config->set_item('enable_query_strings', TRUE);
		$this->pagination->initialize($config);
 		
		$this->load->view($this->uri->segment(1).'/listing', $data);
	}
	
	function priority($ext = '')
	{
		check_permission("agent", "add,edit");
		
		$ext = (string)$ext;
		$error_arr = array();
		$data = array();
		$data['ext'] = $ext;
		$agent = $this->agent->getDetailByID( $ext );
		if( !isset($agent['agent']) ) {
			redirect("/access_denied");	
		}
		
 		$agent_name =  $agent['agent']. " ".(trim($agent['full_name']) != '' ) ? "(" .$agent['agent']. ")" : '';
		$data['page_heading'] = 'Priority for agent'.$agent_name;
		$data['agent_name'] = $agent_name;
		
		if( $this->input->post('sbmt') == 1 ) {
			
			$POST = $this->input->post(NULL, TRUE);
			$skill_error_arr = $this->validate_agent_skills_2($POST);
			if( count($skill_error_arr) > 0 and is_array($skill_error_arr) ) {
				foreach($skill_error_arr as $err) {
					$error_arr[] = $err;	
				}
				unset($err);
			}
			if( count($error_arr) > 0 )
			{
				
			}
			else 
			{
				$this->agent->save_skill_priority($POST);	
				$this->session->set_flashdata('userMesg', 'Record(s) has been updated successfully.');
				redirect($this->uri->segment(1).'/priority/'.$ext);
			}
			
		}
		
		$data['error_arr'] = $error_arr;
		$data['results'] = $this->agent->getAgentSkills("Agent/".$agent['agent']);
		$this->load->view($this->uri->segment(1).'/priority', $data);
	}
	
	
	function save($ext = '') 
	{
		$ext = (string)$ext;
		$POST = $this->input->post(NULL, TRUE);
		$data = array();
		
		if( $ext == '' ) {
			redirect("/access_denied");
		}
		check_permission("agent", "edit");
		
		
		$error_arr = array();
		$data['page_heading'] = 'Agent Skill Assignment';
		$data['all_skills'] = getAllskills();
		$detail = $this->agent->getDetailByID( $ext );
		if( !isset($detail['agent']) ) {
			redirect("/access_denied");	
		}


		foreach($detail as $k=>$v):
		$data[$k] = $v;
		endforeach;
		$data['ext'] = $ext;
		$data['agent_skills'] = $this->agent->getAgentSkills("Agent/".$data['agent']);
		$agent_skill_arr = array();
		if( count($data['agent_skills']) > 0 ) {
			foreach($data['agent_skills'] as $row) {
				$agent_skill_arr[] = $row->queue_name;	
			}	
		}
		$data['agent_skill_arr'] = $agent_skill_arr;
		
		
		if( $this->input->post('sbmt') == 1 )
		{
			if( ! $this->input->post('skills') ) {
				$error_arr[] = 'Please select atleast one skill';	
			}
			if( ! $this->input->post('primary_skill') ) {
				$error_arr[] = 'Please select primary skill';	
			}
			
			$skill_error_arr = $this->validate_agent_skills($POST);
			if( count($skill_error_arr) > 0 and is_array($skill_error_arr) ) {
				foreach($skill_error_arr as $err) {
					$error_arr[] = $err;	
				}
				unset($err);
			}
 		
			if( count($error_arr) > 0 )
			{
				$data['error_arr'] = $error_arr;
				$this->load->view($this->uri->segment(1).'/save', $data);
			}
			else
			{
				$detail = $this->agent->getDetailByID($ext);
				$agent = $detail['agent'];
				$POST['agent'] = $agent;
				$this->agent->save_agent_skills($POST);
				$this->session->set_flashdata('userMesg', 'Record has been updated successfully.');
				redirect($this->uri->segment(1).'/save/'.$ext);
			}
		}
		else
		{
			$data['error_arr'] = $error_arr;
			$this->load->view($this->uri->segment(1).'/save', $data);
		}
			
		
	}// end save
  
 	function check_duplicate($agent, $id ="")
    {
		$agent = "Agent/$agent";
		$id = (int)$id;
		$where = " agent  = '".mysql_real_escape_string($agent)."' ";
		if( $id > 0 ) {
			$where .= " AND agent_id <> ".$id	;
		}
 		if(! $this->common_model->record_exist("qagent", $where))
		{
			return true;	
		}
		else
		{
			$this->form_validation->set_message('check_duplicate', 'The %s Must be Unique.');
			return false;
		}
    }// end check_duplicate
	
	function validate_agent_skills_2($POST)
	{
		$error_arr = array();
		$uniqueids = $POST['ids'];
		$primary_skill 	= @$POST['primary_skill'];
		$primary_skill = $primary_skill[0];
		@list($primary_skill, $primary_skill_id) = explode('~', $primary_skill);
		$primary_skill_id = (int)$primary_skill_id;
		
		if( intval($primary_skill_id) < 1 ) 
		{
			$error_arr[] = 'Please select primary skill';		
			return $error_arr;
		}
		
		$priority_arr = array();
 		$less_than_zero = false;
		$less_than_other_priority = false;
		$have_duplicate = false;
			
		if( count($uniqueids) > 0 ) 
		{
			foreach($uniqueids as $id)
			{
				if( intval($POST['penalty'][$id]) < 1 ) {
					$less_than_zero = true;
				}
				
				if( $id != $primary_skill_id && intval($POST['penalty'][$id]) < intval($POST['penalty'][$primary_skill_id]) )
				{
					$less_than_other_priority = true;
				}
				
				if( intval($POST['penalty'][$id]) > 0 ) {
					$priority_arr[] = intval($POST['penalty'][$id]);
				} 
			}//end foreach 
		}
		
		
		if( $less_than_zero) {
			$error_arr[] = 'Priority must be greater than zero';	
		}
		if( $less_than_other_priority) {
			$error_arr[] = 'Other priority must be greater than primary skill priority';
		}
		
		if( intval($POST['penalty'][$primary_skill_id]) != 10 )
		{
			$error_arr[] = 'Primary skill must have priority 10';	
		}
		
		if( count($priority_arr) != count(array_unique($priority_arr)) ) {
			$error_arr[] = 'Priorities can not be same';
		}
		
		return $error_arr;
	}
	
	
	
	function validate_agent_skills($POST)
	{
		$error_arr = array();
		
		$skills			= @$POST['skills'];
		$primary_skill 	= @$POST['primary_skill'];
		$primary_skill = @$primary_skill[0];
		
		$priority_arr = array();
		
		$less_than_zero = false;
		$less_than_other_priority = false;
		$have_duplicate = false;
 		
		if( count($skills) > 0 ) 
		{
			foreach($skills as $skill)
			{
				if( intval($POST['priority'][$skill]) < 1 ) {
					$less_than_zero = true;
				}
				
				if( $skill != $primary_skill && intval(@$POST['priority'][$skill]) < intval(@$POST['priority'][$primary_skill]) )
				{
					$less_than_other_priority = true;
				}
				
				if( intval($POST['priority'][$skill]) > 0 ) {
					$priority_arr[] = intval($POST['priority'][$skill]);
				} 
				
				
				
			}//end foreach 
		}
		
		if( $less_than_zero) {
			$error_arr[] = 'Priority must be greater than zero';	
		}
		if( $less_than_other_priority) {
			$error_arr[] = 'Other priority must be greater than primary skill priority';
		}
		
		if( intval(@$POST['priority'][$primary_skill]) != 10 )
		{
			$error_arr[] = 'Primary skill must have priority 10';	
		}
		
		if( count($priority_arr) != count(array_unique($priority_arr)) ) {
			$error_arr[] = 'Priorities can not be same';
		}
		
		return $error_arr;
	}

}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */